package com.example.fashionshoes.controller.user;

import com.example.fashionshoes.exception.BusinessException;
import com.example.fashionshoes.pojo.dto.CollectionPageDTO;
import com.example.fashionshoes.pojo.po.Result;
import com.example.fashionshoes.pojo.vo.CollectionVo;
import com.example.fashionshoes.service.CollectionService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/collect")
public class CollectionController {
    @Resource
    private CollectionService collectionService;

    /**
     * 收藏商品
     * @param goodsId 商品id
     * @return null
     */
    @PostMapping("/save")
    public Result<Object> save(@RequestBody Integer goodsId){
        try {
            collectionService.saveCollection(goodsId);
        } catch (BusinessException e) {
            return Result.error("该商品你已收藏");
        }
        return Result.success();
    }

    /**
     * 获取收藏商品列表
     * @param collectionPageDTO 分页数据
     * @return 收藏商品列表
     */
    @PostMapping
    public Result<CollectionVo> list(@RequestBody CollectionPageDTO collectionPageDTO){
        CollectionVo collectionVot = collectionService.getPageCollectionList(collectionPageDTO);
        return Result.success(collectionVot);
    }

    /**
     * 取消商品收藏
     * @param id 收藏商品的id
     * @return null
     */
    @PostMapping("/cancel")
    public Result<Object> cancel(@RequestBody Integer id){
        collectionService.removeById(id);
        return Result.success();
    }
}
